package cn.edu.ren.factory;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @PackageName: cn.edu.ren.factory
 * @ClassName: MyGlobalFilter
 * @Description:
 * @author: RZS
 * @date: 2021/7/6  18:15
 */
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {

    @Override //过滤器的逻辑方法
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("-----------------全局过滤器MyGlobalFilter---------------------");
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if (StringUtils.isBlank(token)) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    //过滤器执行方法
    public int getOrder() {
        //值越小越先执行
        return 1;
    }
}